package com.one.config;

import com.one.constant.RabbitMQConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created on 2022-01-03.
 * <p>
 * Author: zhukang
 * <p>
 * Description: rabbitmq直连模式自动配置类
 */
@Configuration
public class RabbitMQDirectConfig {

    /**
     * @author : zhukang
     * @date : 2022-01-03
     * @return :
     * @description : 配置直连队列，要设置消息持久化
     */
    @Bean
    public Queue directQueue(){

        // 创建消息队列构造：Queue(String name, boolean durable, boolean exclusive, boolean autoDelete)
        // String name : the name of the queue.
        // 当前队列的名称，参数必传，创建队列必须指定队列名
        // boolean durablej : true if we are declaring a durable queue (the queue will survive a server restart)
        // 是否持久化消息，如果是true，自动持久化到磁盘，重启服务，队列中的消息仍然可用，不会丢失
        // boolean exclusive : true if we are declaring an exclusive queue (the queue will only be used by the declarer's connection)
        // 是否是排他队列，默认是false，如果是true，只有创建当前队列的连接才有权限使用，当连接断开之后，队列会自动删除
        // boolean autoDelete : true if the server should delete the queue when it is no longer in use
        // 是否是自动删除队列，默认是false，如果是true，一旦没有生产者或者消费者使用该队列，就会自动删除
        return new Queue(RabbitMQConstant.RABBITMQ_DIRECT_QUEUE_KH91, true);
    }

    /**
     * @author : zhukang
     * @date : 2022-01-03
     * @return :
     * @description : 配置直连交换机
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(RabbitMQConstant.RABBITMQ_DIRECT_EXCHANGE_KH91);
    }

    /**
     * @author : zhukang
     * @date : 2022-01-03
     * @return :
     * @description : 配置直连交换机和直连队列的绑定路由键
     */
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(directQueue()).to(directExchange()).with(RabbitMQConstant.RABBITMQ_DIRECT_ROUTING_KH91);
    }

    /**
     * @author : zhukang
     * @date : 2022-01-03
     * @return :
     * @description : 配置直连交换机，不绑定队列
     */
    @Bean
    public DirectExchange directExchangeOnly(){
        return new DirectExchange(RabbitMQConstant.RABBITMQ_DIRECT_EXCHANGE_ONLY_KH91);
    }

}
